package com.acmen.ump.core.validation;

import com.acmen.ump.core.exception.RequestParamNotValidException;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Validator;

/**
 * @author Acmen
 * @ClassName ValidatorBinder.java
 * @Description 参数验证，validator分布构建工具
 * @CreateTime 2019年08月23日 16:23:23
 */
public class ValidatorBinder extends DataBinder {
    /**
     * 创建构建工具 需要指定验证对象
     */
    public ValidatorBinder(Object target) {
        super(target);
    }

    /**
     * 验证 获取验证结果
     */
    public void validate() {

        for (Validator validator : super.getValidators()) {
            validator.validate(getTarget(), getBindingResult());
        }

        // 判断api请求字段业务验证，有错误抛出
        if (super.getBindingResult().hasErrors()) {
            throw new RequestParamNotValidException(super.getBindingResult());
        }
    }

    /**
     * 添加验证器
     */
    public DataBinder addMoreValidators(Validator... validators) {
        super.addValidators(validators);
        return this;
    }
}
